#include "../base/map.h"
#include <assert.h>
#include <stdio.h>

static const char* _map_key( size_t idx ) {
	static char buf[1024];
#ifdef __linux__
	snprintf( buf , 1024 , "  abc%d" , idx*idx );
#elif _WIN32
	_snprintf( buf , 1024 , "  abc%d" , idx*idx );
#else
	/* prefer leave #else there to make the code change in future */
#endif
	return buf;
}

static int c=0;

static void _print_each( struct map_kv* kv , void* arg ) {
	printf( "key:%s --- val:%d\n" , kv->key , *(int*)(kv->val) );
	++c;
}
void test_1(void) {
	struct map* m = map_create( sizeof(int) , 4 , NULL );
	struct map_kv kv;
	int i = 0 ;
	assert(m);
	// inserting some shit into the map 
	for( i = 0 ; i < 10 ; ++i ) {
		kv.key = _map_key(i);
		kv.val = &i;
		map_insert(m,&kv,NULL);
	}
	// query operation goes here
	for( i = 0 ; i < 10 ; ++i ) {
		int* val = (int*)map_query(m,_map_key(i),NULL);
		printf("%d\n",*val);
		assert( i == *val );
	}
	map_foreach( m , _print_each , NULL);
	assert( c == 10 );
	map_destroy(m);
}

void test_2() {
	struct map* m = map_create( sizeof(int) , 10 , NULL );
	struct map_kv kv;
	int i = 0 ;
	assert(m);
	// inserting some shit into the map 
	for( i = 0 ; i < 1000; ++i ) {
		kv.key = _map_key(i);
		kv.val = &i;
		map_insert(m,&kv,NULL);
	}
	// remove operation goes here
	for( i = 0 ; i < 1000 ; ++i ) {
		assert( map_remove(m,_map_key(i)) == true );
	}
	assert( map_get_usesize(m) == 0 );
	map_destroy(m);
}

void test() {
	test_1();
	test_2();
	printf("map unitest completely!");
}